package com.wh.webflux.controller;

import com.wh.webflux.pojo.Post;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.File;

/**
 * @description:
 * @author: GooGoo
 * @time: 2023/4/10 11:24
 */
@RestController
@RequestMapping("/webflux")
public class HelloController {

    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just("hello spring webflux");
    }

    @GetMapping("/posts")
    public Flux<Post> posts() {
        WebClient webClient = WebClient.create();
        Flux<Post> postFlux = webClient.get().uri("https://jsonplaceholder.typicode.com/posts").retrieve().bodyToFlux(Post.class);
        return postFlux;
    }

    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux() {
        Flux<String> map = Flux.fromArray(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}).log().map(s -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "<letter" + s + ">";
        });
        return map;

    }

}
